//
//  MNDModifyUserNameViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/9/26.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDModifyUserNameViewController.h"
#import "UIImage+JKColor.h"

@interface MNDModifyUserNameViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;

@property(nonatomic, copy) NSString *userName;

@end

@implementation MNDModifyUserNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.submitButton setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    [self.submitButton setBackgroundImage:[UIImage jk_imageWithColor:kColorOrange] forState:UIControlStateNormal];
    
    RAC(self, userName) = self.userNameTextField.rac_textSignal;
    
    RAC(self.submitButton, enabled) = [RACSignal combineLatest:@[RACObserve(self, userName)] reduce:^id (NSString *userName){
        
        return @(userName.length >= 4 && userName.length <= 12);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitAction:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"设置成功后，用户名不可修改，确定提交？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [NetworkProgressHUD showHud];
        
        WeakSelf(self);
        NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_name": self.userName}];
        [APIManager sendRequestWithPath: API_MODIFY_USER_NAME
                             parameters: params
                                 method: kXMHTTPMethodPOST
                              onSuccess: ^(ResponseModel *responseModel) {
                                  
                                  if (responseModel.status == ResponseSuccess) {
                                      [NetworkProgressHUD showHudMsg:@"设置成功"];
                                      
                                      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.85 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                          [weakself.navigationController popViewControllerAnimated:YES];
                                      });
                                  } else {
                                      NSDictionary *dict = responseModel.msg;
                                      if (dict && dict.count > 0) {
                                          [NetworkProgressHUD showHudMsg:dict[@"error"]];
                                      } else {
                                          [NetworkProgressHUD showHudMsg:@"设置用户名失败，请重试"];
                                      }
                                  }
            
                              } onFailure:^(NSError *error) {
                                  [NetworkProgressHUD showHudNetworkError:error];
                              }];
    }];
    
    [alert addAction: cancelAction];
    [alert addAction:action];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
